﻿define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Mypbuy_edit, GoPicture,GoPage, $stateParams, GoDialog, GoAddress, GoCatTypeAuth,$timeout) {

        //数据模型
        $scope.input = new Object();
        $scope.input.id = $stateParams.id; //接受参数
        $scope.initialization_input  = function(){//提交完，要初始化所有参数
        	$scope.input.title = "";
	        $scope.input.end_time = "";
	        $scope.input.address_id = ""; //收货地址
	        $scope.input.add_fp=false;//控制选中
	        $scope.input.normal_fp=false; //控制选中
	        $scope.input.isHide = false;//隐藏发票
	        $scope.input.invoice_type = 3; //默认不要发票。1：专用发票  2：普通发票  3：不用发票
	        $scope.input.phone = "";
	        $scope.input.memo = "";
	        $scope.input.goods = new Array(); //商品数组，多个商品，要提交的，以下数据都放到该数组里
        }
        
		$scope.initialization_good = function(){//添加完一次，就要初始化所有参数，故将他们放到方法体里
			$scope.goods = new Object(); //每点击一次加号，就将改对象push到$scope.input.goodsArray里
	        $scope.goods.name = "";
	        $scope.goods.describe = "";
	        $scope.goods.cat_type_id = "";
	        $scope.goods.brand_need=0;//默认0,即不指定品牌
	        $scope.goods.brand_name="";
	        $scope.goods.count = "";
	        $scope.goods.unit = "";
	        $scope.goods.photo=new Object();//一个求购商品只能有一个图片
            $scope.showImg = new Object();//显示用
            $scope.input.cat1 = {id:0,name:"请选择大分类"};
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
            $scope.goodsfalg=false;//是否有正在编辑的求购商品
		}
       	$scope.initialization_good();
       	$scope.initialization_input();
       	
       	
       	 $scope.getInvoice_type = function(val) { //发票选择
            if(val == 1) {
                $scope.input.add_fp=true;
                $scope.input.normal_fp=false;;
            } else {
                $scope.input.normal_fp=true;
                $scope.input.add_fp=false;
            }
            $scope.input.invoice_type = val;
        }

		$scope.$watch("input.isHide",function(newVal, oldVal){
			if(!newVal){
				$scope.input.invoice_type = 3; //默认不要发票
				$scope.input.normal_fp=false;
                $scope.input.add_fp=false;
			}
		})
       	
        //获取地域。name/name/name:id
        $scope.regionid = function() {
            if($scope.input.district) {
                $scope.input.address_id = $scope.input.province.name + "/" + $scope.input.city.name + "/" + $scope.input.district.name + ":" + $scope.input.district.id;
            } else if($scope.input.city) {
                $scope.input.address_id = $scope.input.province.name + "/" + $scope.input.city.name + ":" + $scope.input.city.id;
            } else if($scope.input.province) {
                $scope.input.address_id = $scope.input.province.name + ":" + $scope.input.province.id;
            }
        }
        //获取分类。name/name/name:id
        $scope.getCat = function(){
        	if($scope.input.cat3) {
                $scope.goods.cat_type_id = $scope.input.cat1.name + "/" + $scope.input.cat2.name + "/" + $scope.input.cat3.name + ":" + $scope.input.cat3.id;
            } else if($scope.input.cat2) {
                $scope.goods.cat_type_id = $scope.input.cat1.name + "/" + $scope.input.cat2.name + ":" + $scope.input.cat2.id;
            } else if($scope.input.cat1) {
                $scope.goods.cat_type_id = $scope.input.cat1.name + ":" + $scope.input.cat1.id;
            }
        }
         
        //动态响应
        $scope.doShowFile = function() {
            document.getElementById("upfilebutton").click();
        }
        //图片上传:上传成功后，会返回图片的地址和图片id
        $scope.addStoreImg = function() {
            GoPicture.upload(function(res) {
                if(res.success) {
                    $scope.showImg.image =res.result.image_src ; //图片 显示用
             		$scope.showImg.id = res.result.image_id; //图片 id显示用
             		$scope.goods.photo = $scope.showImg;
                }else{
                	GoDialog.toast(res.msg);
                }
            });
        }
        
        
        //添加商品
        $scope.addGoods = function() {
            if(!$scope.goods.name) {
                GoDialog.toast("请输入商品名称！");
            } else if(!$scope.goods.count) {
                GoDialog.toast("请输入采购量！");
            } else {
                if(!$scope.goods.brand_need) {//如果不指定品牌，则brand_name为空
                    $scope.goods.brand_name="";
                } 
                $scope.getCat();
                $scope.input.goods.push($scope.goods);
            	$scope.initialization_good();//添加完  要初始化所有数据
            }
        }
        
        //编辑已经添加的商品
        $scope.cat_type_id = 0;//修改绑定用
        $scope.editAlreadyGoods = function(index) {
            if($scope.goodsfalg){
               GoDialog.toast("请先保存目前正在编辑的商品！");
                return;
            }
            $scope.goods = $scope.input.goods[index];
            $scope.showImg = $scope.goods.photo;
            if($scope.goods.cat_type_id){//存在分类时，才进行编辑绑定
				$scope.cat_type_id = $scope.goods.cat_type_id.substring($scope.goods.cat_type_id.indexOf(":")+1);//含头不含尾
            	GoCatTypeAuth.inti_cat($scope, $scope.cat_type_id, function(val1, val2, val3) {
                    //使用定时器来指引
                    $scope.isUnwatch = true; //不进行侦听操作
                    $scope.input.cat1 = val1;
                    $scope.input.cat2 = val2;
                    $scope.input.cat3 = val3;
                    $timeout(function() {
                        $scope.isUnwatch = false; //不进行侦听操作
                    }, 1000);
           		});
            }
            $scope.input.goods.splice(index, 1);//先暂时删除一个
            $scope.goodsfalg=true;
        }
        
        //删除已经添加的商品
        $scope.deleteAlreadyGoods = function(index) {
            $scope.input.goods.splice(index, 1); //删除一个元素
        }

	/*------------地域三级联动------------*/	
    $scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
    $scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
    $scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动

        $scope.listen_province = function(data) {
            $scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
            $scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
        };
        $scope.listen_city = function(data) {
            $scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
        };
        //定义绑定
        var items = new Array();
        var obj1 = {
            name: 'input.province'
        };
        var obj2 = {
            name: 'input.city'
        };
        var obj3 = {
            name: 'input.district'
        };
        items.push(obj1);
        items.push(obj2);
        items.push(obj3);
        GoAddress.init_load($scope, items);
        $scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
	/*------------地域三级联动------------*/	

        //求购保存	new Date($scope.input.end_time).Format("yyyy-MM-dd hh:mm:ss")
        $scope.save = function() {
            if(!$scope.input.title) {
                GoDialog.toast("请输入标题！");
            } else if($scope.input.goods.length == 0) {
                GoDialog.toast("请添加求购商品！");
            } else {
                $scope.regionid();
                Mypbuy_edit.save_getlist($scope.input.title, $scope.input.id, $scope.input.end_time, $scope.input.address_id,
                    $scope.input.invoice_type, $scope.input.phone,
                    $scope.input.memo, $scope.input.goods,
                    function(data) {
                        if(data.success) {
                            GoDialog.toast("保存数据成功！");
       						$scope.initialization_input();//提交完，要初始化参数
                            GoPage.go_states("pbuy");
                        } else {
                            GoDialog.toast(data.msg);
                        }
                    });
            }
        }

        //初始化数据
        $scope.init_data = function() {
            Mypbuy_edit.update_goods($scope.input.id, function(data) {
                $scope.input = data;
               
                //先清空==地区初始化
                $scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
                $scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
                $scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
                GoAddress.init_region($scope, data.address, function(val1, val2, val3) {
                    //使用定时器来指引
                    $scope.isUnwatch = true; //不进行侦听操作
                    $scope.input.province = val1;
                    $scope.input.city = val2;
                    $scope.input.district = val3;
                    $timeout(function() {
                        $scope.isUnwatch = false; //不进行侦听操作
                    }, 1000);
                });
               
                //初始判断选择发票类型
                if(data.invoice_type != 3) {
                    $scope.input.isHide = true;
                }
                if(data.invoice_type == 1) {
                    $scope.input.add_fp=true;
                }else if(data.invoice_type == 2){
                     $scope.input.normal_fp=true;
                }
                
            });
        };
        
        if($scope.input.id) {
            $scope.init_data();
        }
        
        $scope.openFileActSheet = function() {
            var buttons = new Array();
            buttons.push({ text: '删除图片', style: '' });
            buttons.push({ text: '显示大图', style: '' });

            GoDialog.sheet('图片操作', '取消', buttons, function(bindex) {
                if(bindex == 0) {
                    GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
                        $timeout(function() {
                            $scope.showImg = null;
                        }, 1);
                    })
                }else if(bindex == 1) {
                	
                }
                return true;
            });
        }


        /***分类三级联动-无须和品牌关联***/
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
     
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        //定义绑定
        var items_cat = new Array();
        var obj1_cat = {
            name: 'input.cat1'
        };
        var obj2_cat = {
            name: 'input.cat2'
        };
        var obj3_cat = {
            name: 'input.cat3'
        };
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load_only($scope, items_cat);
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
        //取消
        $scope.tui = function() {
            window.history.go(-1);
        }


    }

    controller.$inject = ['$scope', 'Mypbuy_edit', 'GoPicture','GoPage', '$stateParams', 'GoDialog', 'GoAddress', 'GoCatTypeAuth','$timeout'];

    return app.registerController("mypbuy_editctrl",controller);
});